package com.icbc.controller;

import org.testng.Assert;
import org.testng.annotations.Test;
import sun.net.www.protocol.http.HttpURLConnection;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.nio.charset.StandardCharsets;


public class HelloControllerTest {
    @Test(invocationCount = 4)
    public void testHello() throws IOException {
        URL url = new URL("http://localhost:8080/hello");
        HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
        httpURLConnection.setRequestMethod("GET");
        httpURLConnection.setRequestProperty("Content-type", "application/json;charset=GBK");
        int responseCode = httpURLConnection.getResponseCode();
        StringBuffer buffer = new StringBuffer();
        if (responseCode == java.net.HttpURLConnection.HTTP_OK) {
            InputStream inputStream = httpURLConnection.getInputStream();
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8));
            String line = "";
            while ((line = reader.readLine()) != null) {
                buffer.append(line);
            }
        }

        Assert.assertEquals(buffer.toString(), "hello");
    }
}
